package com.yztc.administrator.meishi.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.yztc.administrator.meishi.R;
import com.yztc.administrator.meishi.bean.MyUser;
import com.yztc.administrator.meishi.bean.NewUser;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends AppCompatActivity {

    private EditText etUser;
    private EditText etPassword;
    private EditText etSMS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Bmob.initialize(this,"2cd4546b029739c36ed15d8633533b56");
        //初始化et
        init();
    }

    /* *
       * @author: ZTC
       * @Description: 初始化控件
       * @data:  2016/9/23  9:47
       * @version: V1.0
       */
    private void init() {
        etUser = (EditText) findViewById(R.id.et_reg_user);
        etPassword = (EditText) findViewById(R.id.et_reg_password);
        etSMS = (EditText) findViewById(R.id.et_reg_sms);
    }

    public void register(View view) {
        NewUser user = new NewUser();
        user.setMobilePhoneNumber(etUser.getText().toString());//设置手机号码（必填）
       //设置用户名，如果没有传用户名，则默认为手机号码
        user.setPassword(etPassword.getText().toString());  //设置用户密码
        user.setAge(18); //设置额 外信息：此处为年龄
        user.signOrLogin(etSMS.getText().toString(), new SaveListener<MyUser>() {

            @Override
            public void done(MyUser user,BmobException e) {
                if(e==null){
                    Toast.makeText(RegisterActivity.this, "注册成功"+e.getMessage(), Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(RegisterActivity.this, "注册失败 "+e.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }

        });
    }



    public void sendSMS(View view) {
        String phone = etUser.getText().toString();
        Log.e("TGA","电话号码"+phone);
        BmobSMS.requestSMSCode(phone,"ztc", new QueryListener<Integer>() {
            @Override
            public void done(Integer smsId,BmobException ex) {
                if(ex==null){//验证码发送成功
                    Toast.makeText(RegisterActivity.this, "短信发送成功", Toast.LENGTH_SHORT).show();
                    Log.i("smile", "短信id："+smsId);//用于查询本次短信发送详情
                }else{
                    Toast.makeText(RegisterActivity.this, "短信发送失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
